<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="header-logo">
        <img src="../assets/logo.png" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleBtn">
          <i class="el-icon-bicycle icon-one" v-show="!iscollapse"></i>
          <i class="el-icon-bicycle icon-two" v-show="iscollapse"></i>
        </div>
        <el-menu
          background-color="#847c7c"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="iscollapse"
          :collapse-transition="false"
          router
        >
          <el-submenu :index="item.id + ''" v-for="item of menulist" :key="item.id">
            <!-- 一级菜单模板区 -->
            <template slot="title">
              <i :class="iconsObj[item.id]" style="color: #EEEDF1"></i>
              <span>{{item.authName}}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index=" '/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <i class="el-icon-menu" style="color: #EFE3C5"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      iconsObj: {
        '125': 'el-icon-s-custom',
        '103': 'el-icon-upload',
        '101': 'el-icon-s-goods',
        '102': 'el-icon-s-order',
        '145': 'el-icon-s-data'
      },

      //   是否折叠
      iscollapse: false
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮隐藏展示菜单
    toggleBtn() {
      this.iscollapse = !this.iscollapse
    }
  }
}
</script>

<style lang="stylus" scoped>
.home-container {
  height: 100%;

  .el-header {
    background-color: #545454;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;

    .header-logo {
      display: flex;
      align-items: center;

      span {
        padding-left: 2px;
      }
    }
  }

  .el-aside {
    background-color: #847c7c;

    .el-menu {
      border-right: none;
    }

    .toggle-button {
      background: #9a8e8b;
      line-height: 24px;
      font-size: 25px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;

      .icon-two {
        transform: rotateY(180deg);
      }
    }
  }

  .el-main {
    background-color: #eeedf1;
  }
}
</style>